package com.flxz.user.controller;

import com.flxz.common.annotation.Log;
import com.flxz.common.core.controller.BaseController;
import com.flxz.common.core.domain.AjaxResult;
import com.flxz.common.core.page.TableDataInfo;
import com.flxz.common.enums.BusinessType;
import com.flxz.user.domain.UserIncome;
import com.flxz.user.service.IUserIncomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户收入Controller
 * 
 * @author flxz
 * @date 2024-01-15
 */
@RestController
@RequestMapping("/user/income")
public class UserIncomeController extends BaseController
{
    @Autowired
    private IUserIncomeService userIncomeService;

    /**
     * 查询用户收入列表
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:income:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserIncome userIncome)
    {
        startPage();
        Long userId = getUserId();
        userIncome.setUserId(userId);
        List<UserIncome> list = userIncomeService.selectUserIncomeList(userIncome);
        return getDataTable(list);
    }

    /**
     * 获取用户收入详细信息
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:income:query')")
    @GetMapping(value = "/{incomeId}")
    public AjaxResult getInfo(@PathVariable("incomeId") Long incomeId)
    {
        return success(userIncomeService.selectUserIncomeByIncomeId(incomeId));
    }

    /**
     * 新增用户收入
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:income:add')")
    @Log(title = "用户收入", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserIncome userIncome)
    {
        Long userId = getUserId();
        userIncome.setUserId(userId);
        return toAjax(userIncomeService.insertUserIncome(userIncome));
    }

    /**
     * 修改用户收入
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:income:edit')")
    @Log(title = "用户收入", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserIncome userIncome)
    {
        Long userId = getUserId();
        userIncome.setUserId(userId);
        return toAjax(userIncomeService.updateUserIncome(userIncome));
    }

    /**
     * 修改用户收入（通过ID）
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:income:edit')")
    @Log(title = "用户收入", businessType = BusinessType.UPDATE)
    @PutMapping("/{incomeId}")
    public AjaxResult editById(@PathVariable Long incomeId, @RequestBody UserIncome userIncome)
    {
        Long userId = getUserId();
        userIncome.setUserId(userId);
        userIncome.setIncomeId(incomeId);
        return toAjax(userIncomeService.updateUserIncome(userIncome));
    }

    /**
     * 删除用户收入
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:income:remove')")
    @Log(title = "用户收入", businessType = BusinessType.DELETE)
    @DeleteMapping("/{incomeIds}")
    public AjaxResult remove(@PathVariable Long[] incomeIds)
    {
        return toAjax(userIncomeService.deleteUserIncomeByIncomeIds(incomeIds));
    }

    /**
     * 删除单个用户收入
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:income:remove')")
    @Log(title = "用户收入", businessType = BusinessType.DELETE)
    @DeleteMapping("/{incomeId}")
    public AjaxResult removeSingle(@PathVariable Long incomeId)
    {
        return toAjax(userIncomeService.deleteUserIncomeByIncomeId(incomeId));
    }
}
